//
//  UserInfoViewController.m
//  A_Project_DLS151129_SQ
//
//  Created by 151129-49宋强 on 16/1/25.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "UserInfoViewController.h"
#import "UserInfoTableViewCell.h"
#import "UserWebViewController.h"

#define Width self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface UserInfoViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)NSDictionary *userInfo;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataArr;
@end

@implementation UserInfoViewController
- (void)dealloc
{   [_tableView release];
    [_userInfo release];
    [super dealloc];
}
//页面将要出现隐藏navi
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self data];//数据处理
    [self creatTableView];//创建tableView;
}
#pragma mark - 创建tableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 180, Width, HEIGHT - 180) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    [_tableView release];
    //注册重用池
    [_tableView registerClass:[UserInfoTableViewCell class] forCellReuseIdentifier:@"cell"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
#pragma mark - cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    Model *m = _dataArr[indexPath.row];
    cell.timeLabel.text = m.addtime_f;
    cell.contentLabel.text = m.content;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Model *m = _dataArr[indexPath.row];
    CGFloat h = [UserInfoTableViewCell heightWithText:m.content];
    return 80 + h;
}
#pragma mark - tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Model *m = _dataArr[indexPath.row];
    ReadWebVC *webVc = [[ReadWebVC alloc] init];
    webVc.contenId = m.contentid;
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController pushViewController:webVc animated:YES];
    [webVc release];
}

#pragma mark - 数据处理
- (void)data
{
    NSString *urlStr = @"http://api2.pianke.me/profile/info";
    NSDictionary *dic = @{@"uid": self.uid, @"client": @2};
    [PWNetWorking POSTWithURL:urlStr body:dic completion:^(id result) {
        self.userInfo = result[@"data"][@"userinfo"];
        [self creatUserInfo];//创建用户界面
        NSArray *arr = result[@"data"][@"list"];
        self.dataArr = [Model modelHanderWithArray:arr];
        if (_dataArr.count == 0) {
            UILabel *contentLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 80)];
            contentLable.numberOfLines = 0;
            contentLable.text = @"这个人很懒\n什么都没留下";
            contentLable.center = self.view.center;
            [self.view addSubview:contentLable];
            [contentLable release];
        }
        [self.tableView reloadData];
        }];
}
#pragma mark - 创建用户界面
- (void)creatUserInfo
{
    UIImageView *userImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Width, 150)];
    [userImgView sd_setImageWithURL:[NSURL URLWithString:_userInfo[@"coverimg"]]];
    userImgView.userInteractionEnabled = YES;
    [self.view addSubview:userImgView];
    [userImgView release];
    UIImageView *headImgView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 60, 60)];
    headImgView.layer.cornerRadius = 30;
    [headImgView sd_setImageWithURL:[NSURL URLWithString:_userInfo[@"icon"]]];
    [userImgView addSubview:headImgView];
    [headImgView release];
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(headImgView.frame.size.width + 40 + 5, 40, Width - 20 - 60 - 5, headImgView.frame.size.height)];
    nameLabel.text = _userInfo[@"uname"];
    nameLabel.textColor = [UIColor whiteColor];
    [userImgView addSubview:nameLabel];
    [nameLabel release];
    UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(nameLabel.frame.origin.x, nameLabel.frame.origin.y + nameLabel.frame.size.height - 20, Width - nameLabel.frame.origin.x - 10, 30)];
    descLabel.numberOfLines = 2;
    descLabel.font = [UIFont systemFontOfSize:12];
    descLabel.textColor = [UIColor whiteColor];
    descLabel.text = _userInfo[@"desc"];
    [userImgView addSubview:descLabel];
    [descLabel release];
    UILabel *allLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 200, 30)];
    allLabel.text = @"全部片刻";
    [userImgView addSubview:allLabel];
    [allLabel release];
#pragma mark - 关注 私信 i 三个button
    for (NSInteger i= 0 ; i < 3; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 1000 + i;
        if (i == 0) {
            btn.frame = CGRectMake(headImgView.frame.origin.x, headImgView.frame.origin.y + headImgView.frame.size.height + 10, 40, 30);
            [btn setImage:[UIImage imageNamed:@"guanzhu.png"] forState:UIControlStateNormal];
            [userImgView addSubview:btn];
        }else if (i == 1){
            btn.frame = CGRectMake(Width - 140, headImgView.frame.origin.y + headImgView.frame.size.height + 10, 40, 30);
            [btn setImage:[UIImage imageNamed:@"youjian.png"] forState:UIControlStateNormal];
            [userImgView addSubview:btn];
        }else{
            btn.frame = CGRectMake(Width - 60, headImgView.frame.origin.y + headImgView.frame.size.height + 10, 40, 30);
            [btn setImage:[UIImage imageNamed:@"i.png"] forState:UIControlStateNormal];
            [userImgView addSubview:btn];
        }
        [btn addTarget:self action:@selector(threeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(10, 10, 30, 30);
    [backBtn setImage:[UIImage imageNamed:@"zuo.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [userImgView addSubview:backBtn];
    
    
}
#pragma mark - 三个button触发方法
- (void)threeBtnAction:(UIButton *)btn
{
    if (btn.tag == 1000) {
        NSLog(@"关注");
    }else if (btn.tag == 1001){
        NSLog(@"私信");
    }else{
        UserWebViewController *userWEbVC = [[UserWebViewController alloc] init];
        self.navigationController.navigationBarHidden = NO;
        [self.navigationController pushViewController:userWEbVC animated:YES];
        userWEbVC.uid = self.uid;
        [userWEbVC release];
    }
}
#pragma mark - 返回按钮
- (void)backAction:(UIButton *)btn
{
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
